<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>
    <h1>取消请求</h1>
    <button id="btn">点击</button>
    <button id="btnOne">取消请求</button>


    <script>
        const oBtn = document.getElementById('btn');
        const btnOne = document.getElementById('btnOne')
        const CancelToken = axios.CancelToken;

        let cancel =null
        //创建 axios 的实例
        const creatorAxios = axios.create({
            baseURL: '/',
            timeout: 10000,
            header: {}
        });





        oBtn.onclick = async function () {
            try {
                const re = await creatorAxios.get('/userOne', {
                    params: {
                        userId: '001'
                    },
                    CancelToken : new CancelToken((c)=>{
                       cancel = c;
                        console.log(cancel);
                    })
                })
                console.log(re);
            } catch (e) {
                console.log('e',e);
            }
        }

        btnOne.onclick =function(){
            console.log(1111);
            cancel();
            console.log(cancel);
        }
    </script>
</body>

</html>